import 'package:flutter/material.dart';

// 任务状态枚举
enum TaskStatus { todo, inProgress, completed }

// 任务优先级枚举
enum TaskPriority { low, medium, high }

/// 首页示例4 - 极简风格
class HomePage9 extends StatelessWidget {
  const HomePage9({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return _buildContent(context);
  }
  
  // 构建内容区域
  Widget _buildContent(BuildContext context) {
    return Container(
      color: Colors.grey[50],
      child: SingleChildScrollView(
        padding: const EdgeInsets.all(24.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            _buildHeader(context),
            const SizedBox(height: 32),
            _buildProjectsGrid(),
            const SizedBox(height: 32),
            _buildTasksSection(),
          ],
        ),
      ),
    );
  }
  
  // 构建页面标题
  Widget _buildHeader(BuildContext context) {
    final now = DateTime.now();
    final hour = now.hour;
    
    String greeting;
    if (hour < 12) {
      greeting = '早上好';
    } else if (hour < 18) {
      greeting = '下午好';
    } else {
      greeting = '晚上好';
    }
    
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          '$greeting，管理员',
          style: const TextStyle(
            fontSize: 28,
            fontWeight: FontWeight.bold,
            letterSpacing: -0.5,
          ),
        ),
        const SizedBox(height: 8),
        Text(
          '首页示例9 - 极简风格',
          style: TextStyle(
            fontSize: 16,
            color: Colors.grey[600],
          ),
        ),
      ],
    );
  }
  
  // 构建项目网格
  Widget _buildProjectsGrid() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              '项目概览',
              style: TextStyle(
                fontSize: 20,
                fontWeight: FontWeight.bold,
              ),
            ),
            Text(
              '查看全部',
              style: TextStyle(
                color: Colors.blue,
                fontWeight: FontWeight.w500,
              ),
            ),
          ],
        ),
        const SizedBox(height: 16),
        LayoutBuilder(
          builder: (context, constraints) {
            final crossAxisCount = constraints.maxWidth < 600 ? 1 : 
                                   constraints.maxWidth < 900 ? 2 : 3;
            
            return GridView.count(
              crossAxisCount: crossAxisCount,
              crossAxisSpacing: 16,
              mainAxisSpacing: 16,
              shrinkWrap: true,
              physics: const NeverScrollableScrollPhysics(),
              children: [
                _buildProjectCard(
                  title: 'Flutter Miaoyi UI',
                  subtitle: '移动应用框架',
                  progress: 0.65,
                  daysLeft: 12,
                  members: 5,
                  color: Colors.blue,
                  icon: Icons.mobile_friendly,
                ),
                _buildProjectCard(
                  title: '企业官网重构',
                  subtitle: 'Web开发项目',
                  progress: 0.32,
                  daysLeft: 28,
                  members: 4,
                  color: Colors.orange,
                  icon: Icons.web,
                ),
                _buildProjectCard(
                  title: '客户管理系统',
                  subtitle: '企业内部系统',
                  progress: 0.78,
                  daysLeft: 5,
                  members: 7,
                  color: Colors.green,
                  icon: Icons.people_alt,
                ),
              ],
            );
          },
        ),
      ],
    );
  }
  
  // 构建项目卡片
  Widget _buildProjectCard({
    required String title,
    required String subtitle,
    required double progress,
    required int daysLeft,
    required int members,
    required Color color,
    required IconData icon,
  }) {
    return Container(
      padding: const EdgeInsets.all(24),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(16),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.03),
            blurRadius: 12,
            offset: const Offset(0, 3),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Container(
                padding: const EdgeInsets.all(10),
                decoration: BoxDecoration(
                  color: color.withOpacity(0.1),
                  borderRadius: BorderRadius.circular(12),
                ),
                child: Icon(
                  icon,
                  color: color,
                ),
              ),
              Container(
                padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
                decoration: BoxDecoration(
                  color: Colors.grey[100],
                  borderRadius: BorderRadius.circular(12),
                ),
                child: Text(
                  '还剩 $daysLeft 天',
                  style: TextStyle(
                    fontSize: 12,
                    color: Colors.grey[600],
                    fontWeight: FontWeight.w500,
                  ),
                ),
              ),
            ],
          ),
          const SizedBox(height: 16),
          Text(
            title,
            style: const TextStyle(
              fontSize: 18,
              fontWeight: FontWeight.bold,
            ),
          ),
          const SizedBox(height: 4),
          Text(
            subtitle,
            style: TextStyle(
              fontSize: 14,
              color: Colors.grey[600],
            ),
          ),
          const SizedBox(height: 16),
          Row(
            children: [
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          '进度',
                          style: TextStyle(
                            fontSize: 12,
                            color: Colors.grey[600],
                          ),
                        ),
                        Text(
                          '${(progress * 100).toInt()}%',
                          style: const TextStyle(
                            fontSize: 12,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ],
                    ),
                    const SizedBox(height: 8),
                    LinearProgressIndicator(
                      value: progress,
                      backgroundColor: Colors.grey[200],
                      valueColor: AlwaysStoppedAnimation<Color>(color),
                      borderRadius: BorderRadius.circular(4),
                      minHeight: 6,
                    ),
                  ],
                ),
              ),
            ],
          ),
          const SizedBox(height: 16),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              // 模拟团队成员头像
              SizedBox(
                width: 76,
                height: 24,
                child: Stack(
                  children: List.generate(
                    3,
                    (index) => Positioned(
                      left: index * 16.0,
                      child: Container(
                        width: 24,
                        height: 24,
                        decoration: BoxDecoration(
                          color: Colors.primaries[index % Colors.primaries.length],
                          shape: BoxShape.circle,
                          border: Border.all(color: Colors.white, width: 2),
                        ),
                        child: Center(
                          child: Text(
                            String.fromCharCode(65 + index),
                            style: const TextStyle(
                              color: Colors.white,
                              fontSize: 10,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
              ),
              Text(
                '$members 名成员',
                style: TextStyle(
                  fontSize: 12,
                  color: Colors.grey[600],
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
  
  // 构建任务列表部分
  Widget _buildTasksSection() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              '今日任务',
              style: TextStyle(
                fontSize: 20,
                fontWeight: FontWeight.bold,
              ),
            ),
            Chip(
              label: Text('5 项待办'),
              backgroundColor: Colors.blue,
              labelStyle: TextStyle(color: Colors.white),
              padding: EdgeInsets.symmetric(horizontal: 8),
            ),
          ],
        ),
        const SizedBox(height: 16),
        Container(
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(16),
            boxShadow: [
              BoxShadow(
                color: Colors.black.withOpacity(0.03),
                blurRadius: 12,
                offset: const Offset(0, 3),
              ),
            ],
          ),
          child: Column(
            children: [
              _buildTaskItem(
                title: 'UI设计评审会议',
                time: '09:30 - 10:30',
                type: '会议',
                status: TaskStatus.inProgress,
                priority: TaskPriority.high,
              ),
              _buildTaskItem(
                title: '完成首页原型设计',
                time: '今日内',
                type: '设计',
                status: TaskStatus.todo,
                priority: TaskPriority.medium,
              ),
              _buildTaskItem(
                title: '修复导航栏崩溃问题',
                time: '13:00 前',
                type: '开发',
                status: TaskStatus.inProgress,
                priority: TaskPriority.high,
              ),
              _buildTaskItem(
                title: '提交周报',
                time: '17:30 前',
                type: '文档',
                status: TaskStatus.todo,
                priority: TaskPriority.medium,
              ),
              _buildTaskItem(
                title: '组件库单元测试',
                time: '16:00 前',
                type: '测试',
                status: TaskStatus.completed,
                priority: TaskPriority.low,
                showDivider: false,
              ),
            ],
          ),
        ),
      ],
    );
  }
  
  // 构建任务项
  Widget _buildTaskItem({
    required String title,
    required String time,
    required String type,
    required TaskStatus status,
    required TaskPriority priority,
    bool showDivider = true,
  }) {
    // 根据状态获取颜色和文本
    Color getStatusColor() {
      switch (status) {
        case TaskStatus.todo:
          return Colors.grey;
        case TaskStatus.inProgress:
          return Colors.blue;
        case TaskStatus.completed:
          return Colors.green;
      }
    }
    
    String getStatusText() {
      switch (status) {
        case TaskStatus.todo:
          return '待办';
        case TaskStatus.inProgress:
          return '进行中';
        case TaskStatus.completed:
          return '已完成';
      }
    }
    
    // 根据优先级获取颜色和文本
    Color getPriorityColor() {
      switch (priority) {
        case TaskPriority.low:
          return Colors.green;
        case TaskPriority.medium:
          return Colors.orange;
        case TaskPriority.high:
          return Colors.red;
      }
    }
    
    String getPriorityText() {
      switch (priority) {
        case TaskPriority.low:
          return '低';
        case TaskPriority.medium:
          return '中';
        case TaskPriority.high:
          return '高';
      }
    }
    
    return Column(
      children: [
        Padding(
          padding: const EdgeInsets.all(16),
          child: Row(
            children: [
              Checkbox(
                value: status == TaskStatus.completed,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(4),
                ),
                onChanged: (_) {},
              ),
              const SizedBox(width: 12),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      title,
                      style: TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.w500,
                        decoration: status == TaskStatus.completed 
                            ? TextDecoration.lineThrough 
                            : null,
                        color: status == TaskStatus.completed 
                            ? Colors.grey 
                            : Colors.black,
                      ),
                    ),
                    const SizedBox(height: 4),
                    Row(
                      children: [
                        Icon(
                          Icons.access_time,
                          size: 14,
                          color: Colors.grey[600],
                        ),
                        const SizedBox(width: 4),
                        Text(
                          time,
                          style: TextStyle(
                            fontSize: 12,
                            color: Colors.grey[600],
                          ),
                        ),
                        const SizedBox(width: 12),
                        Container(
                          padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
                          decoration: BoxDecoration(
                            color: Colors.grey[200],
                            borderRadius: BorderRadius.circular(4),
                          ),
                          child: Text(
                            type,
                            style: TextStyle(
                              fontSize: 12,
                              color: Colors.grey[700],
                            ),
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
              Column(
                crossAxisAlignment: CrossAxisAlignment.end,
                children: [
                  Container(
                    padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
                    decoration: BoxDecoration(
                      color: getStatusColor().withOpacity(0.1),
                      borderRadius: BorderRadius.circular(4),
                    ),
                    child: Text(
                      getStatusText(),
                      style: TextStyle(
                        fontSize: 12,
                        color: getStatusColor(),
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                  ),
                  const SizedBox(height: 4),
                  Container(
                    padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
                    decoration: BoxDecoration(
                      color: getPriorityColor().withOpacity(0.1),
                      borderRadius: BorderRadius.circular(4),
                    ),
                    child: Text(
                      '优先级: ${getPriorityText()}',
                      style: TextStyle(
                        fontSize: 12,
                        color: getPriorityColor(),
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
        if (showDivider)
          const Divider(
            height: 1,
            indent: 60,
          ),
      ],
    );
  }
} 